package com.test.mytest.socket;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.FillLayout;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class SocketServerTestAPp {

	protected Shell shell;
	private ServerSocket sSocket;
	protected boolean isStarted=true;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			SocketServerTestAPp window = new SocketServerTestAPp();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(450, 300);
		shell.setText("SWT Application");
		shell.setLayout(new GridLayout(1, false));
		
		Composite composite = new Composite(shell, SWT.NONE);
		composite.setLayout(new FillLayout(SWT.HORIZONTAL));
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		Composite composite_1 = new Composite(shell, SWT.NONE);
		composite_1.setLayout(new GridLayout(3, false));
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		Button button = new Button(composite_1, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				startSocketServer();
			}
		});
		button.setText("启动");
		
		Button button_1 = new Button(composite_1, SWT.NONE);
		button_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try {
					sSocket.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});
		button_1.setText("关闭");
		
		Button button_2 = new Button(composite_1, SWT.NONE);
		button_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				for (Socket socket : mapClient.values()) {
					try {
						OutputStream os = socket.getOutputStream();
						os.write("服务器收到了您的消息！！！".getBytes());
						os.flush();
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
				
			}
		});
		button_2.setText("发消息");

	}
	Map<String, Socket> mapClient=new HashMap<>();
	protected void startSocketServer() {
		try {
			sSocket = new ServerSocket(12345);
			new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						System.out.println("启动服务器监听线程");
						while (isStarted) {
							Socket s = sSocket.accept();
							String hostAddress = s.getInetAddress().getHostAddress();
							mapClient.put(hostAddress, s);
							new Thread(new Runnable() {
								@Override
								public void run() {
									System.out.println("启动客户端消息监听线程："+hostAddress);
									Socket socket = mapClient.get(hostAddress);
									while((socket = mapClient.get(hostAddress))!=null){
										try {
											socket.setSoTimeout(120000);
											InputStream is = socket.getInputStream();
											byte[] b = new byte[1024];
											int read = is.read(b);
											if (read>0) {
												System.out.println(new String(b));
											}
										} catch (Exception e) {
											e.printStackTrace();
											mapClient.remove(hostAddress);
											try {
												socket.close();
											} catch (IOException e1) {
												e1.printStackTrace();
											}
										}
									}
								}
							}).start();
							
						}
					} catch (Exception e) {
						e.printStackTrace();
						System.out.println(e.getMessage()+"=="+e.getMessage().contains("socket closed"));
					}
				}
			}).start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
